package com.edu.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.dfa.SensitiveUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.config.UserContext;
import com.edu.boot.entity.Community;
import com.edu.boot.entity.CommunityComment;
import com.edu.boot.entity.CommunityImages;
import com.edu.boot.entity.SensitiveWord;
import com.edu.boot.service.CommunityCommentService;
import com.edu.boot.service.CommunityImagesService;
import com.edu.boot.service.CommunityService;
import com.edu.boot.common.api.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.boot.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 社区帖子控制器
 * 处理所有与社区帖子相关的请求
 */
@RestController
@RequestMapping("/community")
public class CommunityController {
    
    @Autowired
    private CommunityService communityService;
    @Autowired
    private CommunityImagesService communityImagesService;
    
    @Autowired
    CommunityCommentService communityCommentService;
    
    @Autowired
    SensitiveWordService sensitiveWordService;

    /**
     * 发布新帖
     * @param community 帖子信息对象
     * @return 返回操作结果
     */
    @PostMapping("/add")
    public CommonResult createCommunity(@RequestBody Community community) {
        // 调用服务层方法添加新帖子
        communityService.addCommunity(community);
        return CommonResult.success();
    }
    
    /**
     * 更新帖子信息
     * @param community 需要更新的帖子信息
     * @return 返回操作结果
     */
    @PostMapping("/update")
    public CommonResult updateCommunity(@RequestBody Community community) {
        // 调用MyBatis-Plus的更新方法更新帖子信息
        boolean b = communityService.updateById(community);
        return CommonResult.success();
    }
    
    /**
     * 删除指定帖子
     * @param id 帖子ID
     * @return 返回操作结果
     */
    @GetMapping("/delete/{id}")
    public CommonResult deleteCommunity(@PathVariable Integer id) {
        // 根据ID删除帖子
        communityService.removeById(id);
        return CommonResult.success();
    }
    
    /**
     * 删除指定评论
     * @param id 评论ID
     * @return 返回操作结果
     */
    @GetMapping("/deleteComment/{id}")
    public CommonResult deleteComment(@PathVariable Integer id) {
        // 调用评论服务删除指定评论
        communityCommentService.deleteComment(id);
        return CommonResult.success();
    }

    /**
     * 获取帖子详情
     * @param id 帖子ID
     * @return 返回帖子详细信息
     */
    @GetMapping("/get/{id}")
    public CommonResult getCommunityById(@PathVariable Integer id) {
        // 根据ID查询帖子信息
        Community community = communityService.getById(id);
        return CommonResult.success(community);
    }
    
    /**
     * 分页查询帖子列表
     * @param page 页码
     * @param pageSize 每页大小
     * @return 返回分页后的帖子列表
     */
    @GetMapping("/list")
    public CommonResult getCommunityListPage(@RequestParam(name = "page") Integer page,
                                             @RequestParam(name = "pageSize") Integer pageSize) {
        // 调用服务层方法获取分页数据
        IPage<Community> list = communityService.getList(page, pageSize);
        return CommonResult.success(list);
    }
    
    /**
     * 获取帖子详细信息（包含关联数据）
     * @param id 帖子ID
     * @return 返回帖子详细信息
     */
    @GetMapping("/getCommunity/{id}")
    public CommonResult getCommunity(@PathVariable Integer id) {
        // 获取包含关联数据的帖子信息
        Community community = communityService.getCommunity(id);
        return CommonResult.success(community);
    }
    
    /**
     * 添加评论
     * @param comment 评论信息
     * @return 返回操作结果
     */
    @PostMapping("/addComment")
    public CommonResult addComment(@RequestBody CommunityComment comment) {
        // 获取所有敏感词列表
        List<String> words = sensitiveWordService.list()
                .stream()
                .map(item -> item.getWord())
                .collect(Collectors.toList());
        // 初始化敏感词过滤器
        SensitiveUtil.init(words);
        // 过滤评论内容中的敏感词
        String filterRemark = SensitiveUtil.sensitiveFilter(comment.getRemark());
        comment.setRemark(filterRemark);
        // 设置评论用户ID
        comment.setUid(UserContext.getUserId());
        // 设置评论创建时间
        comment.setCreateTime(DateUtil.date());
        // 保存评论到数据库
        communityCommentService.save(comment);
        return CommonResult.success();
    }
}
